//index.js
const app = getApp()
Page({
  data: {
    articleList: [],
    swiperList: [],
    detailList: [],
    message:'加载更多',
    flag: true,
    num:1
  },
  onReachBottom:function(){
    let that=this
    if (that.data.message =='加载更多'){
      that.getMore()
    }
    
  },
  //加载更多
  getMore: function() {
    const that = this
    wx.cloud.callFunction({
      name: 'article',
      data: {
        num: that.data.num,
        database:'article'
      },
      success: res => {
        if(res.result.data.length<10){
          that.setData({
            message:'没有数据了'
          })
        }
        that.setData({
          articleList: that.data.articleList.concat(res.result.data),
          num:that.data.num++
        })
      },
      fail: res => {
        console.log('失败', res)
      }
    })
  },
  //flag的变化
  flagchange:function(e){
    this.setData({
      flag:e.detail
    })
    if(this.data.flag){
      this.setData({
        detailList:[]
      })
    }
  },
  detailList:function(e){
    this.setData({
      detailList:e.detail
    })
  },
  //页面加载
  onLoad: function() {
    wx.showToast({
      title: '正在加载',
      icon: 'loading',
      duration:600
    })
    const that = this
    wx.cloud.callFunction({
      name: 'article',
      data: {
        num: 0,
        database:'article'
      },
      success: res => {
        //console.log(res.result)
        let swiper = []
        let article = []
        for (let item of res.result.data) {
          if (item.swiper) {
            swiper.push(item)
          } else {
            article.push(item)
          }
        }
        that.setData({
          articleList: article,
          swiperList: swiper
        })
      },
      fail: res => {
        console.log('请求失败', res)
      }
    })
  }

})